// LogListDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MoldingOHT.h"
#include "LogListDlg.h"

#include "Util\MinUtilExt.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLogListDlg dialog


CLogListDlg::CLogListDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLogListDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLogListDlg)
	
	//}}AFX_DATA_INIT
}


void CLogListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLogListDlg)
	DDX_Control(pDX, IDB_FROM, m_bFrom);
	DDX_Control(pDX, IDB_TO, m_bTo);
	DDX_Control(pDX, IDB_LISTPGUP, m_bListPGUP);
	DDX_Control(pDX, IDB_LISTPGDN, m_bListPGDN);
	DDX_Control(pDX, IDB_EXIT, m_bExit);
	DDX_Control(pDX, IDB_SEARCH, m_bSearch);
	DDX_Control(pDX, IDC_DATETIME_SEARCH, m_ctrlSearch);
	DDX_Control(pDX, IDC_DATETIME_SEARCH2, m_ctrlSearch1);
	//}}AFX_DATA_MAP
	DDX_Control(pDX, IDC_LOGLIST, m_LogListCtrl);
}


BEGIN_MESSAGE_MAP(CLogListDlg, CDialog)
	//{{AFX_MSG_MAP(CLogListDlg)
	ON_BN_CLICKED(IDB_LISTPGUP, OnListpgup)
	ON_BN_CLICKED(IDB_LISTPGDN, OnListpgdn)
	ON_BN_CLICKED(IDB_SEARCH, OnSearch)
	ON_BN_CLICKED(IDB_EXIT, OnExit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLogListDlg message handlers
BOOL CLogListDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	SetWindowText("Unit Log History");
	ButtonInitial();
	ListCtrlInit();
	
	m_LogListCtrl.SetExtendedStyle(LVS_EX_FLATSB | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_ONECLICKACTIVATE);
	m_strDirectoryName = _T(".\\data\\UnitLogData\\");
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CLogListDlg::OnListpgup() 
{
	// TODO: Add your control notification handler code here
	m_ScrollSize.cx = 0;
	m_ScrollSize.cy = (m_nColumHeight * 30) * -1;
	
	m_LogListCtrl.Scroll(m_ScrollSize);
}

void CLogListDlg::OnListpgdn() 
{
	// TODO: Add your control notification handler code here
	m_ScrollSize.cx = 0;
	m_ScrollSize.cy = (m_nColumHeight * 30);
	
	m_LogListCtrl.Scroll(m_ScrollSize);
}

void CLogListDlg::OnSearch() 
{
	// TODO: Add your control notification handler code here
	m_LogListCtrl.DeleteAllItems();

//	LV_COLUMN lvcolumn;
	LV_ITEM lvitem;
	int	 j, k, l;
	CTime		m_pStartTime;
	CTime		m_pEndTime;
	int	nStartYear, nStartMonth, nStartDay;
	int	nEndYear, nEndMonth, nEndDay;
	CString strFileName = _T("");
	
	m_ctrlSearch.GetTime(m_pStartTime);
	nStartYear = m_pStartTime.GetYear();
	nStartMonth = m_pStartTime.GetMonth();
	nStartDay = m_pStartTime.GetDay();
	
	m_ctrlSearch1.GetTime(m_pEndTime);
	nEndYear = m_pEndTime.GetYear();
	nEndMonth = m_pEndTime.GetMonth();
	nEndDay = m_pEndTime.GetDay();
	
	for(j=nStartYear; j<=nEndYear; j++)
	{
		for(k=nStartMonth; k<=nEndMonth; k++)
		{
			for(l=nStartDay; l<=nEndDay; l++)
			{
				strFileName.Format(_T("%s%04d%02d%02d.log"),m_strDirectoryName, j, k, l);
				
				FILE* fp;
				
				if((fp = fopen(strFileName, "r")) == NULL)
				{	
					MyMessageBox("Log List File that does not read.","Read Error!", M_ICONERROR);	
					return;
				}
				
				lvitem.mask = LVIF_TEXT;
				
				char buf[1000];
				char buf_1[1000]; 
				char buf_2[1000];
				char buf_3[1000];
				char tmp[1000];
				char* p;
				
				for(int i=0;i<200000;i++)
				{
					fgets( buf,1000,fp );
					if( feof(fp) ) break;
					
					lvitem.iItem = i;
					lvitem.iSubItem = 0;
					sprintf(tmp,"%d",i+1);
					lvitem.pszText = tmp;
					m_LogListCtrl.InsertItem(&lvitem);
					
					p = strtok( buf,"," );
					if(p != NULL) strcpy(buf_1, p);
					else strcpy(buf_1, "");
					
					p = strtok(NULL,",");
					if(p != NULL) strcpy(buf_2, p);
					else strcpy(buf_2, "");
					
					p = strtok(NULL,"\n");
					if(p != NULL) strcpy(buf_3, p);
					else strcpy(buf_3, "");
					
					lvitem.iSubItem = 1;
					sprintf(tmp,"%s",buf_1);
					lvitem.pszText = tmp;
					m_LogListCtrl.SetItem(&lvitem);
					
					lvitem.iSubItem = 2;
					sprintf(tmp,"%s",buf_2);
					lvitem.pszText = tmp;
					m_LogListCtrl.SetItem(&lvitem);
					
					lvitem.iSubItem = 3;
					sprintf(tmp,"%s",buf_3);
					lvitem.pszText = tmp;
					m_LogListCtrl.SetItem(&lvitem);
				}
				
				fclose(fp);
				
				CRect tmpRect;
				m_LogListCtrl.GetItemRect(0, &tmpRect, LVIR_LABEL);
				m_nColumHeight = tmpRect.bottom - tmpRect.top;
				
			}
		}
	}
}

void CLogListDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	m_LogListCtrl.DeleteAllItems();
	CDialog::OnCancel();
}

void CLogListDlg::ButtonInitial()
{
	// Function Button
}

void CLogListDlg::ListCtrlInit()
{
	LV_COLUMN lvcolumn;
//	LV_ITEM lvitem;
	
	char* Title[4] = {"NO", "Day & Time", "Unit", "Data"};
	int width[4] = {40,200,150,290};
	
	m_LogListCtrl.DeleteAllItems();
	for(int i=0;i<4;i++)
	{
		lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
		lvcolumn.fmt = LVCFMT_LEFT;
		lvcolumn.pszText = Title[i];
		lvcolumn.iSubItem = i;
		lvcolumn.cx = width[i];
		m_LogListCtrl.InsertColumn(i,&lvcolumn);
	}
}
